package com.example.mychat.Service;

import com.example.mychat.Dao.wsDao;
import com.example.mychat.Pojo.chatInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Service;

import java.util.List;

@EnableAsync
@Service
@Slf4j
public class wsService {
    @Autowired
    private wsDao wsdao;

    /**
     * 记录数据写入方法执行的次数
     */
    private Integer count;

    /**
     * 异步存储聊天数据
     *
     * @param mes 消息体
     */
    @Async
    public void handleMes(String chatId,String mes) {
//        String chatid = wsdao.chatIdSelect(toId, sendId);
//        if(chatid!=null ||chatid!=""){
//            wsdao.MesUpdate_person(mes,chatid);
//            log.info("数据插入成功"+count++);
//        }else{
//            String member=sendId+","+toId;
//        }
        String count=wsdao.selectChatId(chatId);
        log.info("数据存储");
        if(count!=null || count!=""){
            wsdao.MesUpdate_person(mes,chatId);
//            log.info("数据插入成功"+count++);
        }else{
            log.info("未查询到该联系人");
        }
    }
    public List<chatInfo> getfriendchatdata(String myId){
        log.info("id:"+myId);
        List<chatInfo> chatList=wsdao.selectdata(myId);
        log.info("查询用户chat--service,条数："+chatList.size());
        chatList.forEach(chatInfo -> {
            log.info(chatInfo.toString());
        });
        return chatList;
    }

}
